package org.example.servicemanage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.example.model.manage.bean.Result;
import org.example.model.manage.bean.po.Major;
import org.example.model.manage.bean.po.security.User;
import org.example.model.student.bean.Task;
import org.example.servicemanage.security.MyUserDetails;
import org.example.servicemanage.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/user/counselor")
@PreAuthorize("hasAnyAuthority('counselor')")
public class CounselorController {

    @Autowired
    private StudentService studentService;

    @Autowired
    private CounselorService counselorService;

    @Autowired
    private MajorService majorService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private CounselorMajorService counselorMajorService;

    @GetMapping("/majorList")
    public Result majorList() {
        User u = ((MyUserDetails)SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getUser();
        List<Major> list = counselorMajorService.getMajorByCounselorId(u.getTypeId());
        return Result.success(list);
    }

    @GetMapping("/getTask")
    public Result getTask(Integer majorId) {
        List<Task> list = taskService.getByMajorId(majorId);
        return Result.success(list);
    }

    @GetMapping("/getByMajor")
    public Result countByMajor(Integer majorId) {
        return Result.success(studentService.getAllByMajorId(majorId));
    }

    @GetMapping("/countByMajor")
    public Result countByMajor() {
        return Result.success(studentService.countByMajor());
    }

    @PostMapping("/sendTask")
    public Result sendTask(@RequestBody Task task) {

    }


    @PutMapping("/changeTask")
    public Result changeTask(@RequestBody Task task) {
        int num = taskService.changeTask(task);
        return num == 1 ? Result.success(null) : Result.error(400, "修改失败");
    }

    @DeleteMapping("/deleteTask")
    public Result deleteTask(Integer majorId, Integer taskId) {
        int num = taskService.deleteTask(majorId, taskId);
        return num == 1 ? Result.success(null) : Result.error(400, "删除失败");
    }

}
